package day3;

import org.junit.Test;

import java.util.Scanner;

public class HomeWork {
//    1、输出一下结构：
//    1
//    12
//    123
//    1234
//    12345
    @Test
    public void test1() {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }
            System.out.println();
        }
    }

//    -----*
//    ----***
//    ---*****
//    --*******
//    -*********
//    ***********
    @Test
    public void test2() {
        // i=1  j:1   j:5
        // i=2  j:3   j:4
        // i=3  j:5   j:3
        // i=4  j:7
        // j=2*i-1
        for (int i = 1; i <= 6; i++) {
            // i=1代表打印第一行
            // 打印第一行的“-”
            for (int j = 1; j <= 6 - i ; j++) {
                System.out.print("-");
            }
            // 打印第一行的“*”
            for (int j = 1; j <= (2 * i-1); j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    // 计算1-100以内所有奇数的和以及所有偶数的和，分别打印出来。
    @Test
    public void test4() {
        int oddSum = 0;
        int evenSum = 0;
        for (int i = 1; i <= 100; i++) {
            if (i % 2 == 0) {
                evenSum += i;
            } else {
                oddSum += i;
            }
        }
        System.out.println("奇数： " + oddSum);
        System.out.println("偶数： " + evenSum);
    }

    // 用for循环输出1—1000之间能被5整除的数，且每行输出3个
    @Test
    public void test44() {
        int count = 0;
        for (int i = 1; i <= 1000; i++) {
            if (i % 5 == 0) {// 找到了一个被5整除的数
                System.out.print(i + " ");
                count++;

                /*if (count == 3) {
                    System.out.println();
                    count = 0;
                }*/
                if (count % 3 == 0) {
                    System.out.println();
                }
            }
        }
    }

    // 计算9的阶乘
    @Test
    public void test45() {
        // 9!=9*8*7*6*5*4*3*2*1
        int sum = 1;
        for (int i = 9; i >= 1; i--) {
            sum = sum * i;
        }
        System.out.println("sum: " + sum);
    }

    // 使用循环打印100-200之间所有的素数（只能被1和自己整除的数叫素数）
    @Test
    public void test33() {
        for (int i = 100; i <= 200; i++) {
            // i=100  判断100是不是素数
            // i=101  判断101是不是素数
            boolean isSuShu = true;// 初始认为是素数
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    isSuShu = false;
                    break;
                }
            }
            if (isSuShu/* == true*/) {
                System.out.println(i + "是素数");
            }
        }
    }

//    16、题目：一个5位数，判断它是不是回文数。即12321是回文数，个位与万位相同，十位与千位相同。
//    char[] array = {'a' , 'b', 'c', 'b' , 'a'};
    @Test
    public void test34() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要判断的字符串：");
        String str = scanner.next();// "abcba";
        char[] array = str.toCharArray(); // ['a' , 'b', 'c', 'b' , 'a']
        /*for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }*/

        boolean isHuiWen = true;
        for (int i = 0; i < array.length / 2; i++) {
            if (array[i] != array[array.length - i - 1]) {
                isHuiWen = false;
                break;
            }
        }

        if (isHuiWen) {
            System.out.println("是回文");
        } else {
            System.out.println("不是回文");
        }
    }

    // int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
    @Test
    public void test133() {
        int[] scores = {0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        int[] counts = new int[10];
        for (int i = 0; i < scores.length; i++) {// 30
            for (int j = 0; j <= 9; j++) {// 10
                if (scores[i] == j) {
                    counts[j]++;
                    break;
                }
            }
        }

        for (int i = 0; i < counts.length; i++) {
            System.out.println(i + "出现了" + counts[i] + "次");
        }
    }

    @Test
    public void test132() {
        int[] scores = {0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        int[] counts = new int[10];
        for (int i = 0; i < scores.length; i++) {// 30
            switch (scores[i]) {
                case 0:
                    counts[scores[i]]++;
                    break;
                case 1:
                    counts[scores[i]]++;
                    break;
                case 2:
                    counts[scores[i]]++;
                    break;
                case 3:
                    counts[scores[i]]++;
                    break;
                case 4:
                    counts[scores[i]]++;
                    break;
                case 5:
                    counts[scores[i]]++;
                    break;
                default:
                    break;
            }
        }

        for (int i = 0; i < counts.length; i++) {
            System.out.println(i + "出现了" + counts[i] + "次");
        }
    }

    @Test
    public void test134() {
        int[] scores = {0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        int[] counts = new int[10];
        for (int i = 0; i < scores.length; i++) {// 30
            switch (scores[i]) {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                    counts[scores[i]]++;
                    break;
                default:
                    break;
            }
        }

        for (int i = 0; i < counts.length; i++) {
            System.out.println(i + "出现了" + counts[i] + "次");
        }
    }

    @Test
    public void test135() {
        int[] scores = {0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        int[] counts = new int[10];
        for (int i = 0; i < scores.length; i++) {// 30
            counts[scores[i]]++;
        }

        for (int i = 0; i < counts.length; i++) {
            System.out.println(i + "出现了" + counts[i] + "次");
        }
    }

//    int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
//    要求求出其中的奇数个数和偶数个数。
    @Test
    public void test234() {
        int[] scores = {0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        int evenCount = 0;
        int oddCount = 0;
        for (int i = 0; i < scores.length; i++) {
            if (scores[i] % 2 == 0) {
                evenCount++;
            } else {
                oddCount++;
            }
        }
        System.out.println("奇数个数： " + oddCount);
        System.out.println("偶数个数： " + evenCount);
    }

//    3、输入一组学生的成绩，使用数组，然后计算他们的平均值.
//    int[] scores = new int[3];
    @Test
    public void test78() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学生数量");
        int count = scanner.nextInt();
        int[] scores = new int[count];
        for (int i = 0; i < scores.length; i++) {
            System.out.println("请输入第" + (i + 1) + "个学生成绩");
            scores[i] = scanner.nextInt();
        }

        int sum = 0;
        for (int i = 0; i < scores.length; i++) {
            sum += scores[i];
        }

        int avg = sum / scores.length;
        System.out.println("平均值： " + avg);

    }

//    17、输入一行字符，分别统计出其中英文字母、空格、数字和其它字符的个数。
    @Test
    public void test89() {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        System.out.println(str);
        char[] chars = str.toCharArray();
        int countLetter = 0;
        int countSpace = 0;
        int countDigit = 0;
        int countOther = 0;
        // hsi3fdfg gudw093
        for (int i = 0; i < chars.length; i++) {
            if ((chars[i] >= 'a' && chars[i] <= 'z')
                    || (chars[i] >= 'A' && chars[i] <= 'Z')) {
                countLetter++;
            } else if (chars[i] == ' ') {
                countSpace++;
            } else if (chars[i] >= '0' && chars[i] <= '9') {
                countDigit++;
            } else {
                countOther++;
            }
        }
        System.out.println("字母出现的次数： " + countLetter);
        System.out.println("空格出现的次数： " + countSpace);
        System.out.println("数字出现的次数： " + countDigit);
        System.out.println("其他出现的次数： " + countOther);
    }

}
